import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-07-03
 * Time：09:29
 */
public class GridTest {

    public static boolean backspaceCompare(String s, String t) {
        Stack<Character> s1 = new Stack<>();
        Stack<Character> s2 = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char tmp = s.charAt(i);
            if (tmp != '#') {
                s1.push(tmp);
            } else {
                if(s1.empty()) {
                    continue;
                }
                s1.pop();
            }
        }
        for(int i = 0; i < t.length(); i++) {
            char tmp = t.charAt(i);
            if (tmp != '#') {
                s2.push(tmp);
            } else {
                if(s2.empty()) {
                    continue;
                }
                s2.pop();
            }
        }

        boolean flag = true;

        //元素不同的情况
        while(!s1.empty() && !s2.empty()) {
            if (s1.pop() != s2.pop()) {
                flag = false;
            }
        }

        //数目不同的情况
        if((s1.empty() && !s2.empty()) || (!s1.empty() && s2.empty())) {
            flag = false;
        }

        return flag;
    }

    public static List<List<Integer>> shiftGrid(int[][] grid, int k) {
        List<List<Integer>> ret = new ArrayList<>();
        //网格行数
        int rowNum = grid.length;
        //网格列数
        int colNum = grid[0].length;
        //初始化结果为全0
        for(int i = 0; i < rowNum; i++) {
            List<Integer> row = new ArrayList<>();
            for(int j = 0; j < colNum; j++) {
                row.add(0);
            }
            ret.add(row);
        }
        for(int i = 0; i < rowNum; i++) {
            for(int j = 0; j < colNum; j++) {
                //转化为一维数组并且迁移后的坐标
                int index = (i * colNum + j + k) % (rowNum * colNum);
                //对应结果的横坐标
                int retx = index % colNum;
                int rety = index / colNum;
                //将原网格的结果设置到新结果上
                ret.get(rety).set(retx, grid[i][j]);
            }
        }
        return ret;
    }

    public static void main(String[] args) {
//        int[][] grid = {{1}, {2}, {3}, {4}, {7}, {6}, {5}};
//        List<List<Integer>> ret = shiftGrid(grid, 23);
//        System.out.println(ret);

        boolean flag = backspaceCompare("ab#c", "ad#c");
        System.out.println(flag);
    }
}
